//
//  InputWifiViewController.m
//  AILock
//
//  Created by 谭亚明 on 2020/6/18.
//  Copyright © 2020 tym. All rights reserved.
//

#import "InputWifiViewController.h"
#import "ScanDevViewController.h"
#import "UIView+BackGround.h"
#import "ESPTools.h"
#import "UIButton+ImageTitleSpacing.h"
#import "UIButton+Gradient.h"
#import <CoreLocation/CoreLocation.h>
#import "AFNetworking.h"

@interface InputWifiViewController ()

@property (nonatomic, strong)NSDictionary *netInfo;

@property (nonatomic, strong)CLLocationManager *locationManagerSystem;
@end

@implementation InputWifiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"添加设备"];
    [self bgViewSet];
    
    [self userLocationAuth];
    
   // if (self.equipModel.peiwangType == 0) {
        //程序进入前台并处于活动状态调用
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wifiViewUpdates) name:UIApplicationDidBecomeActiveNotification object:nil];
        //注册Wi-Fi变化通知
        AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
        [manager startMonitoring];
        [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            [self wifiViewUpdates];
        }];
   // }
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (![self isOpenLocation]) {
        [WHToast showMessage:@"定位权限未开启" duration:2 finishHandler:nil];
    }
}

- (void)userLocationAuth {
    if (![self getUserLocationAuth]) {
        _locationManagerSystem = [[CLLocationManager alloc]init];
        _locationManagerSystem.delegate = self;
        [_locationManagerSystem requestWhenInUseAuthorization];
    }
}

-(BOOL)isOpenLocation{
    if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized)) {

    return YES;

    }else if ([CLLocationManager authorizationStatus] ==kCLAuthorizationStatusDenied) {

        return NO;

    }
return NO;
}

- (BOOL)getUserLocationAuth {
    BOOL result = NO;
    switch ([CLLocationManager authorizationStatus]) {
        case kCLAuthorizationStatusNotDetermined:
            break;
        case kCLAuthorizationStatusRestricted:
            break;
        case kCLAuthorizationStatusDenied:
            break;
        case kCLAuthorizationStatusAuthorizedAlways:
            result = YES;
            break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            result = YES;
            break;
            
        default:
            break;
    }
    return result;
}

-(void)bgViewSet{
    self.bgView.layer.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor;
    self.bgView.layer.cornerRadius = 15;
    self.bgView.layer.shadowColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:0.5].CGColor;
    self.bgView.layer.shadowOffset = CGSizeMake(0,-6.5);
    self.bgView.layer.shadowOpacity = 1;
    self.bgView.layer.shadowRadius = 14;
    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
    [rightBtn setImage:[UIImage imageNamed:@"icon_login6"] forState:UIControlStateNormal];
    [rightBtn setImage:[UIImage imageNamed:@"icon_login9"] forState:UIControlStateHighlighted];
    rightBtn.selected = YES;
    self.wifipassFid.rightViewMode = UITextFieldViewModeAlways;
    [rightBtn addTarget:self action:@selector(showPasswordOrHidden:) forControlEvents:UIControlEventTouchUpInside];
    self.wifipassFid.rightView = rightBtn;
    
    [self.wifiNameFid addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.wifipassFid addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    
    [self.yesBtn gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)rgba(139, 175, 255),(id)rgba(76, 132, 255)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
    self.yesBtn.layer.masksToBounds = YES;
    self.yesBtn.layer.cornerRadius = 5;
    self.yesBtn.alpha=5;//透明度
    self.yesBtn.userInteractionEnabled = NO;
}

-(void)showPasswordOrHidden:(UIButton*)sender{
    
    if (sender.isSelected) {
        [sender setImage:[UIImage imageNamed:@"icon_login9"] forState:UIControlStateNormal];
        [self.wifipassFid setSecureTextEntry:NO];
    }else{
        [sender setImage:[UIImage imageNamed:@"icon_login6"] forState:UIControlStateNormal];
        [self.wifipassFid setSecureTextEntry:YES];
    }
    sender.selected = !sender.selected;
}

- (void)wifiViewUpdates {
    
    self.netInfo = [self fetchNetInfo];
    self.wifiNameFid.text = self.netInfo[@"ssid"];
    
}

- (NSDictionary *)fetchNetInfo
{
    NSMutableDictionary *wifiDic = [NSMutableDictionary dictionaryWithCapacity:0];
    wifiDic[@"ssid"] = ESPTools.getCurrentWiFiSsid;
    wifiDic[@"bssid"] = ESPTools.getCurrentBSSID;
    return wifiDic;
}

-(IBAction)scanDeviceFunction:(UIButton*)btn{
    
    if (self.wifipassFid.text.length == 0) {
        [WHToast showMessage:@"请输入WiFi密码!" duration:2 finishHandler:nil];
        return;
    }
    
    ScanDevViewController *devVC = [[ScanDevViewController alloc] init];
    devVC.netInfo = self.netInfo;
    devVC.equipType = self.equipModel;
    devVC.passwordStr = self.wifipassFid.text;
    devVC.wifiName = self.wifiNameFid.text;
    [self.navigationController pushViewController:devVC animated:YES];
}

-(IBAction)changeWifiFunction:(id)sender{
    if (@available(iOS 10.0, *)) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
    } else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }
}

-(void)changedTextField:(UITextField*)textField
{
    if (self.wifiNameFid.text.length > 0 && self.wifipassFid.text.length > 0) {
        self.yesBtn.userInteractionEnabled = YES;
        self.yesBtn.alpha = 1;
    }else{
        self.yesBtn.userInteractionEnabled = NO;
        self.yesBtn.alpha = 0.4;
    }
}


@end
